//
//  TypeViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TypeViewController.h"
#import "Type.h"
#import "TypeCollectionViewCell.h"
#import "KindOfViewController.h"

@interface TypeViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)MBProgressHUD *HUD;


@end

@implementation TypeViewController

- (void)dealloc
{
    [_HUD release];
    [_arr release];
    [_collectionView release];
    [_address release];
    [_type release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [view1 release];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, 44)];
    UIImage *image1 = [UIImage imageNamed:@"bg2.jpg"];
    image1 = [image1 resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    imageView1.userInteractionEnabled = YES;
    imageView1.image = image1;
    [self.view addSubview:imageView1];
    [imageView1 release];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [leftButton addSubview:imageView];
    [imageView release];
    
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageView1 addSubview:leftButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, VIEW_WIDTH - 200, 44)];
    titleLabel.text = self.type;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];
    [titleLabel release];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((VIEW_WIDTH - 41) / 3, (VIEW_HEIGHT - 60 - 64) / 5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT - 64 - (70.0 / 667) * VIEW_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[TypeCollectionViewCell class] forCellWithReuseIdentifier:@"TypeCollection11Reuse"];
    [_collectionView release];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.collectionView];
    [self.collectionView addSubview:self.HUD];
    self.HUD.labelText = @"客官请稍等";
    self.HUD.dimBackground = YES;
    [self.HUD show:YES];
    self.arr = [NSMutableArray array];
    
    
    [NetHandler getDataWithUrl:self.address completion:^(id dict) {
       
        [self haldData:dict];
        [self.HUD hide:YES];
    }];
    
    
}

- (void)haldData:(id)data
{
    NSArray *dataArr = [data objectForKey:@"list"];
    if ([dataArr isKindOfClass:[NSArray class]]) {
    
    for (NSDictionary *dic in dataArr) {
        Type *type = [[Type alloc] initWithDictionary:dic];
        [self.arr addObject:type];
        [type release];
    }
    [self.collectionView reloadData];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeCollection11Reuse" forIndexPath:indexPath];
    cell.type = [self.arr objectAtIndex:indexPath.row];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KindOfViewController *kindOfVC = [[KindOfViewController alloc] init];
    Type *type = [self.arr objectAtIndex:indexPath.row];
    kindOfVC.myTitle = type.tname;
    
    kindOfVC.type = self.type;
    
//    if ([self.type isEqualToString:@"音乐"]) {
//        kindOfVC.address = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=android&page=1&per_page=20&status=0&tag_name=%@", type.tname];
//    }
//    if ([self.type isEqualToString:@"新闻资讯"]) {
//        kindOfVC.address = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=news&condition=hot&device=android&page=1&per_page=20&status=0&tag_name=%@", type.tname];
//    }
    
    [self.navigationController pushViewController:kindOfVC animated:YES];
    [kindOfVC release];
}

- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
